package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Division;
import com.sl.au.entity.EventToEvaluateUser;
import com.sl.au.entity.Grading;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Module;
import com.sl.au.entity.User;
import com.sl.au.entity.ViewForUserScoreAnalysis;
import com.sl.au.service.DivisionService;
import com.sl.au.service.EventToEvaluateUserService;
import com.sl.au.service.GridAreaInfoService;
import com.sl.au.service.GridUserDetailsService;
import com.sl.au.service.ViewForUserScoreAnalysisService;
import com.sl.utils.ExdeUtil;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping(value = "/ui/TowerAnalysis")
public class TowerAnalysisController extends BaseController {

	@Autowired
	ViewForUserScoreAnalysisService viewForUserScoreAnalysisService;
	@Autowired
	EventToEvaluateUserService eventToEvaluateUserService;
	@Autowired
	GridAreaInfoService gridAreaInfoService;
	@Autowired
	DivisionService divisionService;
	@Autowired
	GridUserDetailsService gridUserDetailsService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("analysis/towerAnalysis");
		Module module = this.getPageModule("TowerAnalysis");
		modelAndView.addObject("module", module);
		return modelAndView;
	}

	/**
	 * 获取部件区域树信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<Division> node = null;
		
		node = divisionService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=divisionService.getNodeMap(node);
		
		map.put("code", 1);
		map.put("nodes", nodeMap);
	
		return map;
	}
	
	/**
	 * 获取绑定区域网格的信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getGridAreaPage", method = RequestMethod.POST)
	public Map<String,Object> getGridAreaPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String divisionId=request.getParameter("divisionId");
		String key=request.getParameter("key");
		List<GridAreaInfo> modules=null;
		int count = 0;
		
		modules = gridAreaInfoService.getPageByDivisionAndKey(start, length, divisionId, "网格", key, null, null);
		count = gridAreaInfoService.getCountByDivisionAndKey(divisionId, "网格", key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 获取网格评级层级
	 * @param gridId
	 * @return
	 */
	@RequestMapping(value = "getGrading/{gridId}", method = RequestMethod.POST)
	public Map<String,Object> getGrading(@PathVariable("gridId") String gridId) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Grading> gradings = gridUserDetailsService.getRepository().getGradingByGrid(gridId);
		map.put("gradings", gradings);
		return map;
	}

	@RequestMapping(value = "getUser/{gridId}/{gradingId}", method = RequestMethod.POST)
	public Map<String,Object> getUser(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId, @RequestBody Map<String,Object> map) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String cyc = (String) map.get("cyc");
		String begin = (String) map.get("beginDate");
		String end = (String) map.get("endDate");
		Date beginDate = null;
		Date endDate = null;
		if(cyc.equals("week")) {
			beginDate = ExdeUtil.getThisWeekBegin();
			endDate = ExdeUtil.getThisWeekEnd();
		}else if(cyc.equals("month")) {
			beginDate = ExdeUtil.getThisMonthBegin();
			endDate = ExdeUtil.getThisMonthEnd();
		}else if(cyc.equals("year")) {
			beginDate = ExdeUtil.getThisYearBegin();
			endDate = ExdeUtil.getThisYearEnd();
		}else {
			String pattern = "yyyy-MM-dd HH:mm:ss";
			begin += " 00:00:00";
			end += " 23:59:59";
			beginDate = ExdeUtil.str2Date(begin, pattern);
			endDate = ExdeUtil.str2Date(end, pattern);
			
		}
		List<Map<String, Object>> users  = viewForUserScoreAnalysisService.getTowerUser(beginDate, endDate, gridId, gradingId);
		resultMap.put("users", users);
		return resultMap;
	}
	
	/**
	 * 获取详情列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getEvaluatePage", method = RequestMethod.POST)
	public Map<String, Object> getEvaluatePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		
		
		String dataType = request.getParameter("dataType");
		String dataId = request.getParameter("dataId");
		List<EventToEvaluateUser> pages = null;
		int count = 0;
		if(dataType.equals("")) {
			pages = new ArrayList<EventToEvaluateUser>();
		}else {
			
			String cyc = request.getParameter("cyc");
			String begin = request.getParameter("beginDate");
			String end = request.getParameter("endDate");
			String uId = request.getParameter("uId");
			Date beginDate = null;
			Date endDate = null;
			if(cyc.equals("week")) {
				beginDate = ExdeUtil.getThisWeekBegin();
				endDate = ExdeUtil.getThisWeekEnd();
			}else if(cyc.equals("month")) {
				beginDate = ExdeUtil.getThisMonthBegin();
				endDate = ExdeUtil.getThisMonthEnd();
			}else if(cyc.equals("year")) {
				beginDate = ExdeUtil.getThisYearBegin();
				endDate = ExdeUtil.getThisYearEnd();
			}else {
				String pattern = "yyyy-MM-dd HH:mm:ss";
				begin += " 00:00:00";
				end += " 23:59:59";
				beginDate = ExdeUtil.str2Date(begin, pattern);
				endDate = ExdeUtil.str2Date(end, pattern);
				
			}
			
			pages = eventToEvaluateUserService.getPageByDate(start, length, uId, key, beginDate, endDate, dataType, dataId, null, null);
			count = eventToEvaluateUserService.getCountByDate(uId, key, beginDate, endDate,  dataType, dataId);
		}
		
		map.put("data", pages);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
}
